//
//  FindView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/11.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class FindView: UIView {
    lazy var btn1:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(0, 0, 112, 50)
        btn.backgroundColor = UIColor.clear
        btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        btn.tag = 11
        return btn
    }()
    lazy var btn2:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(0, 50, 112, 50)
        btn.backgroundColor = UIColor.clear
        btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        btn.tag = 12
        return btn
    }()
    lazy var btn3:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(0, 100, 112, 50)
        btn.backgroundColor = UIColor.clear
        btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        btn.tag = 13
        return btn
    }()
    lazy var btn4:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(0, 150, 112, 50)
        btn.backgroundColor = UIColor.clear
        btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        btn.tag = 14
        return btn
    }()
    
    lazy var label1:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(30, 15, 80, 20))
        label.font = Font_Bold(14)
        label.textColor = RGB_Same(53)
        label.backgroundColor = .clear
        label.textAlignment = .center
        label.text = "找相似"
        return label
    }()
    lazy var label2:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(30, 65, 80, 20))
        label.font = Font_Bold(14)
        label.textColor = RGB_Same(53)
        label.backgroundColor = .clear
        label.textAlignment = .center
        label.text = "置顶"
        return label
    }()
    lazy var label3:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(30, 115, 80, 20))
        label.font = Font_Bold(14)
        label.textColor = RGB_Same(53)
        label.backgroundColor = .clear
        label.textAlignment = .center
        label.text = "取消收藏"
        return label
    }()
    lazy var label4:UILabel = {
        let label = UILabel.init(frame: FRAME_CGFloat(30, 165, 80, 20))
        label.font = Font_Bold(14)
        label.textColor = RGB_Same(53)
        label.backgroundColor = .clear
        label.textAlignment = .center
        label.text = "分享"
        return label
    }()
    
    lazy var image1:UIImageView = {
        let imageView = UIImageView(frame: CGRect(x:15, y:20, width:11, height:11))
        imageView.image = IMAGE(name: "history0")
        return imageView
    }()
    lazy var image2:UIImageView = {
        let imageView = UIImageView(frame: CGRect(x:15, y:70, width:11, height:11))
        imageView.image = IMAGE(name: "history1")
        return imageView
    }()
    lazy var image3:UIImageView = {
        let imageView = UIImageView(frame: CGRect(x:15, y:120, width:11, height:11))
        imageView.image = IMAGE(name: "history2")
        return imageView
    }()
    lazy var image4:UIImageView = {
        let imageView = UIImageView(frame: CGRect(x:15, y:170, width:11, height:11))
        imageView.image = IMAGE(name: "history3")
        return imageView
    }()

    
    var dateBlock:((_ num:Int) -> Void)?
    
    var model = ListenListModel()
    var modelList: ListenListModel? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            upUI()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func upUI() {
        if model.markStatus == 0 {
            image3.image = IMAGE(name: "record收藏")
            label3.text = "取消收藏"
        } else {
            image3.image = IMAGE(name: "record已收藏")
            label3.text = "收藏"
        }
    }
    
    func setUI() {
        self.backgroundColor = RGB_Same(0, 0.4)
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(removeAction)))
        
        let view = UIView.init(frame: FRAME_CGFloat(SCREEN_WIDTH - 150, SCREEN_HEIGHT / 2 - 100, 112, 200))
        view.layer.cornerRadius = 3
        view.layer.masksToBounds = true
        view.backgroundColor = .white
        self.addSubview(view)
        for i in 0..<4 {
            let ind = (50 * CGFloat(i))
            
            let backView = UIView.init(frame: FRAME_CGFloat(0, ind, 112, 50))
            if i == 0 || i == 2 {
                backView.backgroundColor = RGB_Same(223)
            } else {
                backView.backgroundColor = UIColor.clear
            }
            view.addSubview(backView)
        }
        view.addSubview(image1)
        view.addSubview(image2)
        view.addSubview(image3)
        view.addSubview(image4)
        view.addSubview(label1)
        view.addSubview(label2)
        view.addSubview(label3)
        view.addSubview(label4)
        view.addSubview(btn1)
        view.addSubview(btn2)
        view.addSubview(btn3)
        view.addSubview(btn4)
        
        /*
        for i in 0..<4 {
            let ind = (50 * CGFloat(i))
            
            let backView = UIView.init(frame: FRAME_CGFloat(0, ind, 112, 50))
            if i == 0 || i == 2 {
                backView.backgroundColor = RGB_Same(223)
            } else {
                backView.backgroundColor = UIColor.clear
            }
            view.addSubview(backView)
            
            let imageView = UIImageView(frame: CGRect(x:15, y:20 + ind, width:11, height:11))
            imageView.image = IMAGE(name: "history\(i)")
            view.addSubview(imageView)
            
            let label = UILabel.init(frame: FRAME_CGFloat(30, 15 + ind, 80, 20))
            label.font = Font_Bold(14)
            label.textColor = RGB_Same(53)
            label.backgroundColor = .clear
            label.textAlignment = .center
            label.text = labelArr[i]
            view.addSubview(label)
            
            let btn = UIButton.init(type: .custom)
            btn.frame = FRAME_CGFloat(0, ind, 112, 50)
            btn.backgroundColor = UIColor.clear
            btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
            btn.tag = 11 + i
            view.addSubview(btn)
        }
 */
        
    }
    
    @objc func removeAction() {
        self.removeFromSuperview()
    }
    
    @objc func btnAction(btn:UIButton) {
        switch btn.tag {
        case 11:
            dateBlock?(btn.tag)
        case 12:
            topMark(completion: { (success) in
                if success {
                    self.dateBlock?(btn.tag)
                }
            })
        case 13:
            if model.markStatus == 0 {
                noCollect(completion: { (success) in
                    if success {
                        self.dateBlock?(btn.tag)
                    }
                })
            } else {
                toCollect(completion: { (success) in
                    if success {
                        self.dateBlock?(btn.tag)
                    }
                })
            }
        case 14:
            dateBlock?(btn.tag)
        default:
            return
        }
        self.removeFromSuperview()
    }
    
    func topMark(completion:@escaping (_ succeed: Bool) -> ()) {
        RecordRequestModel.topMark(parameters: ["userId":String.getUserId(),"programId":model.id]) { (success) in
            completion(success)
        }
    }
    
    func toCollect(completion:@escaping (_ succeed: Bool) -> ()) {
        HomeRequestModel.increaseHistoryAndCollections(parameters: ["userId":String.getUserId(),"status":2,"programId":model.id,"h5Url":""]) { (success) in
            if success {
                TTAlertView.showMessage(message: "收藏成功")
            }
            completion(success)
        }
    }
    func noCollect(completion:@escaping (_ succeed: Bool) -> ()) {
        HomeRequestModel.delHistory(parameters: ["userId":String.getUserId(),"programId":model.id,"h5Url":""]) { (success) in
            if success {
                TTAlertView.showMessage(message: "取消收藏")
            }
            completion(success)
        }
    }
    
}
